package com.efast.cafe.portal.service.api.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;

import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.api.PackagePublishIndentifierBean;
import com.efast.cafe.portal.bean.api.PortalApiObjectBean;
import com.efast.cafe.portal.bean.api.PortalApiObjectDto;
import com.efast.cafe.portal.bean.api.PortalApiObjectQueryBean;
import com.efast.cafe.portal.bean.api.PortalApiProjectBean;
import com.efast.cafe.portal.bean.query.TableConditionBean;
import com.efast.cafe.portal.dao.api.PackagePublishIndentifierDao;
import com.efast.cafe.portal.dao.api.PackagePublishIndentifierDefDao;
import com.efast.cafe.portal.dao.api.PortalApiFieldDao;
import com.efast.cafe.portal.dao.api.PortalApiModObjDefDao;
import com.efast.cafe.portal.dao.api.PortalApiModuleDao;
import com.efast.cafe.portal.dao.api.PortalApiObjectDao;
import com.efast.cafe.portal.dao.api.PortalApiProSysDefDao;
import com.efast.cafe.portal.dao.api.PortalApiProjectDao;
import com.efast.cafe.portal.dao.api.PortalApiSysModDefDao;
import com.efast.cafe.portal.dao.api.PortalApiSystemDao;
import com.efast.cafe.portal.entity.api.PortalApiField;
import com.efast.cafe.portal.entity.api.PortalApiSystem;
import com.efast.cafe.portal.entity.query.TableView;
import com.efast.cafe.portal.entity.query.Template;
import com.efast.cafe.portal.remote.query.QueryTemplateService;
import com.efast.cafe.portal.service.api.IPortalApiObjectService;
import com.efast.cafe.portal.service.query.IQueryTemplateService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * <p>
 * Service接口实现类
 * </p>
 * <p>
 * Table: portal_api_object - 对象定义
 * </p>
 *
 * @since 2017-08-10 02:38:15
 */
@Service
@Transactional
public class PortalApiObjectServiceImpl implements IPortalApiObjectService {

	private PortalApiObjectDao portalApiObjectDao;

	private PortalApiFieldDao portalApiFieldDao;

	private PortalApiModObjDefDao portalApiModObjDefDao;

	private PortalApiModuleDao portalApiModuleDao;

	private PortalApiSystemDao portalApiSystemDao;

	private PortalApiProSysDefDao portalApiProSysDefDao;

	private PortalApiSysModDefDao portalApiSysModDefDao;

	private PortalApiProjectDao portalApiProjectDao;

	private PackagePublishIndentifierDao packagePublishIndentifierDao;

	private PackagePublishIndentifierDefDao packagePublishIndentifierDefDao;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private ObjectRedisTemplate objectRedisTemplate;
	
	@Autowired
	private IQueryTemplateService queryTemplateService;
	
	

	@Autowired
	public void setPortalApiObjectDao(PortalApiObjectDao portalApiObjectDao) {
		this.portalApiObjectDao = portalApiObjectDao;
	}

	@Autowired
	public void setPortalApiFieldDao(PortalApiFieldDao portalApiFieldDao) {
		this.portalApiFieldDao = portalApiFieldDao;
	}

	@Autowired
	public void setPortalApiModObjDefDao(PortalApiModObjDefDao portalApiModObjDefDao) {
		this.portalApiModObjDefDao = portalApiModObjDefDao;
	}

	@Autowired
	public void setPortalApiModuleDao(PortalApiModuleDao portalApiModuleDao) {
		this.portalApiModuleDao = portalApiModuleDao;
	}

	@Autowired
	public void setPortalApiProSysDefDao(PortalApiProSysDefDao portalApiProSysDefDao) {
		this.portalApiProSysDefDao = portalApiProSysDefDao;
	}

	@Autowired
	public void setPortalApiSysModDefDao(PortalApiSysModDefDao portalApiSysModDefDao) {
		this.portalApiSysModDefDao = portalApiSysModDefDao;
	}

	@Autowired
	public void setPortalApiProjectDao(PortalApiProjectDao portalApiProjectDao) {
		this.portalApiProjectDao = portalApiProjectDao;
	}

	@Autowired
	public void setPortalApiSystemDao(PortalApiSystemDao portalApiSystemDao) {
		this.portalApiSystemDao = portalApiSystemDao;
	}

	@Autowired
	public void setPackagePublishIndentifierDao(PackagePublishIndentifierDao packagePublishIndentifierDao) {
		this.packagePublishIndentifierDao = packagePublishIndentifierDao;
	}

	@Autowired
	public void setPackagePublishIndentifierDefDao(PackagePublishIndentifierDefDao packagePublishIndentifierDefDao) {
		this.packagePublishIndentifierDefDao = packagePublishIndentifierDefDao;
	}

	/**
	 * 新增object {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月11日
	 * @see com.efast.cafe.portal.remote.api.PortalApiObjectService#insertApiObject(com.efast.cafe.portal.bean.api.PortalApiObjectBean)
	 *
	 */
	@Override
	public int insertApiObject(PortalApiObjectBean portalApiObjectBean) {
		// TODO Auto-generated method stub
		// 新增object
		portalApiObjectBean.setStatus((byte) 2);
		portalApiObjectBean.setUuid(UUID.randomUUID().toString());
		int count = portalApiObjectDao.objIndentifierCount(portalApiObjectBean);
		PortalApiSystem system = portalApiSystemDao.queryById(portalApiObjectBean.getSystemId());
		if (count != 0) {
			throw new ServiceException("存在相同的标识符，请重新输入");
		}
		if(system==null){
			throw new ServiceException("系统不存在,参数有误!");
		}
//		int countTable = portalApiObjectDao.objTableNameCount(portalApiObjectBean);
//		if (countTable != 0) {
//			throw new ServiceException("存在相同的表名，请重新输入");
//		}
		// 创建
		portalApiObjectDao.insertApiObject(portalApiObjectBean);
		// 如果存在字段信息，新增字段
		if (portalApiObjectBean.getFieldList().size() > 0) {
			portalApiFieldDao.insertApiField(portalApiObjectBean.getId(), portalApiObjectBean.getFieldList(), SpringWebUtils.getRequestUser());
		}
		// 增加module和object关系表信息
		portalApiModObjDefDao.insertApiModObjDef(portalApiObjectBean.getModuleId(), portalApiObjectBean.getId(), SpringWebUtils.getRequestUser());
		
		
		
		//新增默认的search template
		Template template = new Template();
		template.setName(portalApiObjectBean.getObjDesc());
		template.setI18nName(portalApiObjectBean.getObjName());
		template.setModule(portalApiObjectBean.getModuleId().toString());
		template.setVersion(system.getVersion());
		template.setViewComponent(2);
		template.setValid(1);
		template.setCheckboxType(1);
		template.setColumnSelectable(1);
		template.setShowColumnIndex(0);
		template.setShowImportButton(0);
		template.setShowExportButton(0);
		template.setMaxRowsInNoPage(1000);
		template.setPagenation(10);
		
		
		List<TableView> tableViews = new ArrayList<>();
		
		template.setFromArea(" FROM "+portalApiObjectBean.getTableName());
		
		List<PortalApiField> fieldList = portalApiObjectBean.getFieldList();
		StringBuilder selectStr = new StringBuilder("SELECT");
		
		for(int i=0;i<fieldList.size();i++){
			TableView view = new TableView();
			PortalApiField field = fieldList.get(i);
			String keyString = StringUtils.isNotBlank(field.getPhysicalField())?field.getPhysicalField():field.getFieldName();
			String alias = StringUtils.isNotBlank(field.getFieldName())?field.getFieldName():field.getPhysicalField();
			view.setKey(keyString);
			view.setAlias(alias);
			view.setI18nKey(field.getI18nKey());
			view.setEntity(portalApiObjectBean.getIndentifier());
			view.setDefaultView(field.getIsPk()==1?0:1);
			view.setGroupby(0);
			view.setOrderIndex(i);
			tableViews.add(view);
			
			selectStr.append(" ").append(keyString).append(" as ").append(alias);
			if(i!=fieldList.size()-1){
				selectStr.append(",");
			}
		}
		template.setSelectArea(selectStr.toString());
		queryTemplateService.addQueryTemplate(template, tableViews,new ArrayList<TableConditionBean>(), portalApiObjectBean.getIndentifier()+"_QT", null);
		
		
		return portalApiObjectBean.getId();
	}

	private void createTable(String tableName, List<PortalApiField> portalApiFields) {
		// 组织需要插入的字段集合
		StringBuilder fieldSql = new StringBuilder();
		for (PortalApiField portalApiField : portalApiFields) {
			if (portalApiField.getIsPk() == 1) {
				fieldSql.append("`id` int(11) NOT NULL primary key AUTO_INCREMENT,");
			} else {
				Byte fieldType = portalApiField.getFieldType();
				String type = "";
				if (fieldType == 1) {
					type = "varchar";
				} else if (fieldType == 2) {
					type = "int";
				} else if (fieldType == 3) {
					type = "double";
				} else if (fieldType == 4) {
					type = "date";
				} else if (fieldType == 5) {
					type = "boolean";
				} else if (fieldType == 6) {
					type = "file";
				} else if (fieldType == 7) {
					type = "json";
				} else if (fieldType == 8) {
					type = "json";
				}
				fieldSql.append("`" + portalApiField.getFieldName() + "` int(11)  DEFAULT NULL COMMENT '" + portalApiField.getFieldDesc() + "',");
			}
		}
		 
	}

	/**
	 * 修改object {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月12日
	 * @see com.efast.cafe.portal.remote.api.PortalApiObjectService#updateApiObject(com.efast.cafe.portal.bean.api.PortalApiObjectBean)
	 *
	 */
	@Override
	public void updateApiObject(PortalApiObjectBean portalApiObjectBean) {
		int count = portalApiObjectDao.objectVersionIsExistWhenInsert(portalApiObjectBean);
		if (count != 0) {
			throw new ServiceException("已经存在相同版本，请重新输入");
		}
		// 修改object信息
		portalApiObjectDao.updateApiObject(portalApiObjectBean);
		// 删除filed信息
		portalApiFieldDao.deleteApiFieldByObjId(portalApiObjectBean.getId());
		// 如果存在字段信息，新增字段
		if (portalApiObjectBean.getFieldList().size() > 0) {
			portalApiFieldDao.insertApiField(portalApiObjectBean.getId(), portalApiObjectBean.getFieldList(), SpringWebUtils.getRequestUser());
		}
		if(stringRedisTemplate.hasKey(portalApiObjectBean.getIndentifier())){
			stringRedisTemplate.delete(portalApiObjectBean.getIndentifier());
		}
		if(stringRedisTemplate.hasKey(portalApiObjectBean.getTableName())){
			stringRedisTemplate.delete(portalApiObjectBean.getTableName());
		}
		stringRedisTemplate.opsForValue().set(CommonConstant.SESSION_ENTITY_VER, UUID.randomUUID().toString());
	}

	/**
	 * 删除object {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月12日
	 * @see com.efast.cafe.portal.remote.api.PortalApiObjectService#deleteApiObject(com.efast.cafe.portal.bean.api.PortalApiObjectBean)
	 *
	 */
	@Override
	public void deleteApiObject(PortalApiObjectBean portalApiObjectBean) {
		// TODO Auto-generated method stub
		// 删除object信息
		portalApiObjectDao.deleteApiObject(portalApiObjectBean.getId());
		// 删除filed信息
		portalApiFieldDao.deleteApiFieldByObjId(portalApiObjectBean.getId());
	}

	/**
	 * 通过objId查询对象detail信息 {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月12日
	 * @see com.efast.cafe.portal.remote.api.PortalApiObjectService#queryObjByObjId(com.efast.cafe.portal.bean.api.PortalApiObjectBean)
	 *
	 */
	@Override
	public PortalApiObjectBean queryObjByObjId(PortalApiObjectBean portalApiObjectBean) {
		// TODO Auto-generated method stub
		return portalApiObjectDao.queryObjByObjId(portalApiObjectBean.getId());
	}

	/**
	 * 发布object {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月14日
	 * @see com.efast.cafe.portal.remote.api.PortalApiObjectService#publishApiObject(com.efast.cafe.portal.bean.api.PortalApiObjectBean)
	 *
	 */
	@Override
	public void publishApiObject(PortalApiObjectBean portalApiObjectBean) {
		// TODO Auto-generated method stub
		List<Integer> refIds = new ArrayList<Integer>();
		for (int i = 0; i < portalApiObjectBean.getObjList().size(); i++) {
			PortalApiObjectBean apiObjectBean = portalApiObjectDao.queryObjByObjId(portalApiObjectBean.getObjList().get(i).getId());
			apiObjectBean.setStatus((byte) 1);
			// portalApiObjectBean.setObjName(portalApiObjectBean.getObjName().replace("＂",
			// "\""));
			apiObjectBean.setVersion(portalApiObjectBean.getObjList().get(i).getVersion());
			apiObjectBean.setParentLastTime(apiObjectBean.getLastUpdateTime());
			apiObjectBean.setParentId(portalApiObjectBean.getObjList().get(i).getId());
			apiObjectBean.setPublishTime(new Date());
			apiObjectBean.setObjDesc(portalApiObjectBean.getObjList().get(i).getObjDesc());
			int count = portalApiObjectDao.objectVersionIsExistWhenInsert(apiObjectBean);
			if (count != 0) {
				throw new ServiceException("已经存在相同版本，请重新输入");
			}
			portalApiObjectDao.insertApiObject(apiObjectBean);
			// 记录新增的object的id
			refIds.add(apiObjectBean.getId());
			// 如果存在字段信息，新增字段
			if (apiObjectBean.getFieldList().size() > 0) {
				for (int j = 0; j < apiObjectBean.getFieldList().size(); j++) {
					apiObjectBean.getFieldList().get(j).setFieldName(apiObjectBean.getFieldList().get(j).getFieldName().replace("＂", "\""));
				}
				portalApiFieldDao.insertApiField(apiObjectBean.getId(), apiObjectBean.getFieldList(), SpringWebUtils.getRequestUser());
			}
			// 增加module和object关系表信息
			portalApiModObjDefDao.insertApiModObjDef(portalApiObjectBean.getModuleId(), apiObjectBean.getId(), SpringWebUtils.getRequestUser());
		}
		// 新增打包发布标识
		PackagePublishIndentifierBean packagePublishIndentifierBean = new PackagePublishIndentifierBean();
		packagePublishIndentifierBean.setModuleId(portalApiObjectBean.getModuleId());
		packagePublishIndentifierBean.setRefType((byte) 2);
		packagePublishIndentifierBean.setPublishIndentifier(portalApiObjectBean.getPublishIndentifier());
		packagePublishIndentifierBean.setCreateUser(SpringWebUtils.getRequestUser());
		packagePublishIndentifierDao.insertPackagePublishIndentifier(packagePublishIndentifierBean);
		if (!refIds.isEmpty()) {
			// 新增打包标识符与服务的关系
			packagePublishIndentifierDefDao.insertPackagePublishIndentifierDefs(refIds,
			    packagePublishIndentifierBean.getId(), SpringWebUtils.getRequestUser());
		}

	}

	/**
	 * 升级对象 {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月15日
	 * @see com.efast.cafe.portal.remote.api.PortalApiObjectService#upgradeApiObject(com.efast.cafe.portal.bean.api.PortalApiObjectBean)
	 *
	 */
	@Override
	public void upgradeApiObject(PortalApiObjectBean portalApiObjectBean) {
		// TODO Auto-generated method stub
		for (int i = 0; i < portalApiObjectBean.getObjList().size(); i++) {
			PortalApiObjectBean apiObjectBean = portalApiObjectDao.queryObjByObjId(portalApiObjectBean.getObjList().get(i).getId());
			apiObjectBean.setStatus((byte) 2);
			apiObjectBean.setParentLastTime(apiObjectBean.getLastUpdateTime());
			apiObjectBean.setParentId(portalApiObjectBean.getObjList().get(i).getId());
			int count = portalApiObjectDao.objectVersionIsExistWhenInsert(apiObjectBean);
			if (count != 0) {
				throw new ServiceException("已经存在相同版本，请重新输入");
			}
			portalApiObjectDao.insertApiObject(apiObjectBean);
			portalApiObjectBean.getObjList().get(i).setId(apiObjectBean.getId());
			// 如果存在字段信息，新增字段
			if (apiObjectBean.getFieldList().size() > 0) {
				portalApiFieldDao.insertApiField(apiObjectBean.getId(), apiObjectBean.getFieldList(), SpringWebUtils.getRequestUser());
			}
			// 增加module和object的关系
			// portalApiModObjDefDao.insertApiModObjDef(portalApiObjectBean.getModule().getId(),
			// apiObjectBean.getId(), SpringWebUtils.getRequestUser());
		}
		// 判断module是否为null，如果存在，生成新的module
		if (portalApiObjectBean.getModule() != null) {
			portalApiObjectBean.getModule().setCreateUser(SpringWebUtils.getRequestUser());
			portalApiObjectBean.getModule().setLastUpdateUser(SpringWebUtils.getRequestUser());
			int count = portalApiModuleDao.moduleVersionIsExistWhenInsert(portalApiObjectBean.getModule());
			if (count != 0) {
				throw new ServiceException("已经存在相同版本，请重新输入");
			}
			portalApiObjectBean.getModule().setModuleId(portalApiObjectBean.getModule().getId());
			portalApiModuleDao.upgradeApiModule(portalApiObjectBean.getModule());
			// 增加新module和object关系表信息
			portalApiModObjDefDao.upgradeApiModObjDef(portalApiObjectBean.getModule().getId(), portalApiObjectBean.getModuleId(), SpringWebUtils.getRequestUser());
			// portalApiModObjDefDao.insertApiModObjDef(portalApiObjectBean.getModule().getId(),
			// apiObjectBean.getId(), SpringWebUtils.getRequestUser());
			for (int i = 0; i < portalApiObjectBean.getObjList().size(); i++) {
				portalApiModObjDefDao.insertApiModObjDef(portalApiObjectBean.getModule().getId(), portalApiObjectBean.getObjList().get(i).getId(), SpringWebUtils.getRequestUser());
			}

		} else {
			// 增加旧module和新object关系表信息
			for (int i = 0; i < portalApiObjectBean.getObjList().size(); i++) {
				portalApiModObjDefDao.insertApiModObjDef(portalApiObjectBean.getModuleId(), portalApiObjectBean.getObjList().get(i).getId(), SpringWebUtils.getRequestUser());
			}
		}
		// 判断系统是否存在，存在生成新的系统
		if (portalApiObjectBean.getSystem() != null) {
			// 获取系统id
			Integer systemId = portalApiObjectBean.getSystem().getId();
			portalApiObjectBean.getSystem().setStatus((byte) 2);
			// 生成新的系统
			portalApiSystemDao.copyApiSystem(portalApiObjectBean.getSystem());
			// 新系统id
			Integer newsystemId = portalApiObjectBean.getSystem().getId();
			// 复制原系统下的所有模块的关系给新系统
			portalApiSysModDefDao.copyApiSysModDefListTo(systemId, newsystemId, SpringWebUtils.getRequestUser());
			// 模块存在的话，创建新模块与新系统的关系，如果不存在，则不需要
			if (portalApiObjectBean.getModule() != null) {
				// 新增新增模块与系统的信息
				portalApiSysModDefDao.insertApiSysModDef(newsystemId, portalApiObjectBean.getModule().getId(), SpringWebUtils.getRequestUser(), 1);
			} /*
			   * else{ //新增一条新增系统和旧模块的关系信息
			   * portalApiSysModDefDao.copyApiSysModDefListTo(systemId,
			   * portalApiObjectBean.getSystem().getId(),
			   * SpringWebUtils.getRequestUser());
			   * //portalApiSysModDefDao.insertApiSysModDef(portalApiObjectBean.
			   * getSystem().getId(), portalApiObjectBean.getModuleId(),
			   * SpringWebUtils.getRequestUser()); }
			   */
			// 系统不存在新的的时候，是否存在新的模块
		} else {
			// 判断模块是否存在，存在的话，新增一条新增模块与系统的信息，不存在的话，不需要新增
			if (portalApiObjectBean.getModule() != null) {
				// 新增一条新增模块与系统的信息
				portalApiSysModDefDao.insertApiSysModDef(portalApiObjectBean.getModule().getSystemId(), portalApiObjectBean.getModule().getId(), SpringWebUtils.getRequestUser(), 1);
			}
		}
		// 判断是否存在project
		if (portalApiObjectBean.getProject() != null) {
			// 记录需要升级的ProjectId
			Integer oldProjectId = portalApiObjectBean.getProject().getId();
			portalApiObjectBean.getProject().setStatus((byte) 2);
			int count = portalApiProjectDao.projectVersionInsert(portalApiObjectBean.getProject());
			if (count != 0) {
				throw new ServiceException("已经存在相同版本，请重新输入");
			}
			// 升级project
			portalApiProjectDao.insertApiProject(portalApiObjectBean.getProject());
			// 记录升级后的projectId
			Integer newProjectId = portalApiObjectBean.getProject().getId();
			// 通过原来的projectId查询所有的system，构建项目系统关系表信息
			portalApiProSysDefDao.upgradeApiProSysDef(newProjectId, oldProjectId, SpringWebUtils.getRequestUser());
			// 是否存在系统
			if (portalApiObjectBean.getSystem() != null) {
				// 关联新项目新系统的关系
				portalApiProSysDefDao.insertApiProSysDef(newProjectId, portalApiObjectBean.getSystem().getId(), 1, SpringWebUtils.getRequestUser());
			}
		} else {
			// 项目不需要升级，系统需要升级
			if (portalApiObjectBean.getSystem() != null) {
				// 关联原来的项目新系统的关系
				portalApiProSysDefDao.insertApiProSysDef(portalApiObjectBean.getProject().getId(), portalApiObjectBean.getSystem().getId(), 1, SpringWebUtils.getRequestUser());
			}
		}
	}

	/**
	 * 通过projectId查询项目下面所有的object {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月17日
	 * @see com.efast.cafe.portal.remote.api.PortalApiObjectService#queryAllObjectByProId(java.lang.Integer)
	 *
	 */
	@Override
	public PortalApiObjectDto queryAllObjectByProId(PortalApiObjectDto portalApiObjectDto) {
		// TODO Auto-generated method stub
		portalApiObjectDto.setResultList(portalApiObjectDao.queryAllObjectByProId(portalApiObjectDto));
		portalApiObjectDto.setTotalRows(portalApiObjectDao.queryCountObjectByProId(portalApiObjectDto));
		return portalApiObjectDto;
	}

	/**
	 * 通过moduleID查询模块下面的所有的最新object {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月21日
	 * @see com.efast.cafe.portal.remote.api.PortalApiObjectService#queryObjectByModuleId(com.efast.cafe.portal.bean.api.PortalApiObjectDto)
	 *
	 */
	@Override
	public PortalApiObjectDto queryObjectByModuleId(PortalApiObjectDto portalApiObjectDto) {
		// TODO Auto-generated method stub
		portalApiObjectDto.setResultList(portalApiObjectDao.queryObjectByModuleId(portalApiObjectDto));
		portalApiObjectDto.setTotalRows(portalApiObjectDao.queryCountObjectByModuleId(portalApiObjectDto));
		return portalApiObjectDto;
	}

	/**
	 * 通过object的uuid查询所有该模块下的object的版本 {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年8月21日
	 * @see com.efast.cafe.portal.remote.api.PortalApiObjectService#queryObjectByObjcetUUid(com.efast.cafe.portal.bean.api.PortalApiObjectDto)
	 *
	 */
	@Override
	public List<PortalApiObjectDto> queryObjectByObjcetUUid(PortalApiObjectDto portalApiObjectDto) {
		// TODO Auto-generated method stub
		return portalApiObjectDao.queryObjectByObjcetUUid(portalApiObjectDto);
	}

	/**
	 * 基础校验
	 * 
	 * @Date : 2017年9月1日
	 * @param apiObjectBean
	 */
	public void apiObjectBeanCheck(PortalApiObjectBean apiObjectBean) {
		if (apiObjectBean.getObjName().isEmpty() || apiObjectBean.getVersion().isEmpty() || apiObjectBean.getIndentifier().isEmpty()) {
			throw new ServiceException("项目名称、版本、标识符不能为空！");
		}
	}

	/**
	 * 校验object的系统标识符，在syetem下面唯一 {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年9月8日
	 * @see com.efast.cafe.portal.remote.api.PortalApiObjectService#objIndentifierCount(com.efast.cafe.portal.bean.api.PortalApiObjectBean)
	 *
	 */
	@Override
	public void objIndentifierCount(PortalApiObjectBean apiObjectBean) {
		// TODO Auto-generated method stub

		// 外键来源暂定对象标识符
		apiObjectBean.setIndentifier(apiObjectBean.getReferEntity());
		int count = portalApiObjectDao.objIndentifierCount(apiObjectBean);
		if (count == 0) {
			throw new ServiceException("请填写正确的外键来源");
		}

		int referProCount = portalApiObjectDao.objReferProCount(apiObjectBean);
		if (referProCount == 0) {
			throw new ServiceException("请填写正确的外键字段");
		}
	}

	/**
	 * 查询对象列表 {@inheritDoc} overridden:
	 * 
	 * @Date : 2017年9月12日
	 * @see com.efast.cafe.portal.remote.api.PortalApiObjectService#queryAllObjectList(com.efast.cafe.portal.bean.api.PortalApiObjectDto)
	 *
	 */
	@Override
	public PortalApiObjectQueryBean queryAllObjectList(PortalApiObjectQueryBean apiObjectQueryBean) {
		apiObjectQueryBean.setResultList(portalApiObjectDao.queryAllObjectList(apiObjectQueryBean));
		apiObjectQueryBean.setTotalRows(portalApiObjectDao.queryAllObjectListCount(apiObjectQueryBean));
		return apiObjectQueryBean;
	}

	@Override
	public List<PortalApiProjectBean> queryObjectAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PortalApiObjectDto queryObjectBySystemId(PortalApiObjectDto portalApiObjectDto) {
		// TODO Auto-generated method stub
		portalApiObjectDto.setResultList(portalApiObjectDao.queryAllObjectBySystemId(portalApiObjectDto));
		return portalApiObjectDto;
	}

	@Override
	public void updateApiObjectLayout(PortalApiObjectDto portalApiObjectBean) {
		// TODO Auto-generated method stub
		portalApiObjectDao.updateApiObjectLayout(portalApiObjectBean);
		PortalApiObjectBean portalApiObjectBean2 = new PortalApiObjectBean() ;
		portalApiObjectBean2.setId(portalApiObjectBean.getId());
		portalApiObjectBean2 = queryObjByObjId(portalApiObjectBean2) ;
		if(portalApiObjectBean2!=null){
			stringRedisTemplate.delete(portalApiObjectBean2.getIndentifier());
			stringRedisTemplate.delete(portalApiObjectBean2.getTableName());
		}
	}

}